/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EcranImpression.java
 *
 * Created on 8 avr. 2010, 09:18:26
 */

package vue;

import controleur.CCLogger;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import main.Outils;
import vue.composants.EcranImpressionCPD;



/**
 *
 * @author sylvainc
 */
public class EcranImpression extends Ecran {

    private EcranImpressionCPD ecranImpressionCPD;
    public final static String NOM_CARTE_POSTALE = "carte_postale.jpg";

    /** Creates new form EcranImpression */
    public EcranImpression(FenetreMere mere) {
        super(mere);
        initComponents();
        try
        {
            // Création de l'aperçu
            Dimension dim = new Dimension(536, 400);

            this.ecranImpressionCPD = new EcranImpressionCPD(ressources, this, ressources.getCartePostale(), "impression", dim);
            this.panel.add(this.ecranImpressionCPD);

            initialiser();
        }
        catch (Exception ex) {
            CCLogger.Write(Level.SEVERE, ex.getStackTrace().toString());
            this.afficherMessageErreur(ex.getMessage());
        }
    }

    @Override
    public void initialiser(){
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labBandeau = new javax.swing.JLabel();
        btnPrecedent = new javax.swing.JButton();
        btnRecommencer = new javax.swing.JButton();
        labIndication = new javax.swing.JLabel();
        panel = new javax.swing.JPanel();
        btnSuivant = new javax.swing.JButton();
        btnImprimer = new javax.swing.JButton();
        btnSauvegarder = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 204, 255), 2));
        setMaximumSize(new java.awt.Dimension(1024, 768));
        setMinimumSize(new java.awt.Dimension(1024, 768));
        setPreferredSize(new java.awt.Dimension(1024, 768));

        labBandeau.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vue/images/choiximpression.png"))); // NOI18N
        labBandeau.setMaximumSize(new java.awt.Dimension(1024, 88));
        labBandeau.setMinimumSize(new java.awt.Dimension(1024, 88));
        labBandeau.setPreferredSize(new java.awt.Dimension(1024, 88));

        btnPrecedent.setFont(new java.awt.Font("Tahoma", 0, 18));
        btnPrecedent.setText("< Précédent");
        btnPrecedent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrecedentActionPerformed(evt);
            }
        });

        btnRecommencer.setFont(new java.awt.Font("Tahoma", 0, 18));
        btnRecommencer.setText("Recommencer");
        btnRecommencer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRecommencerActionPerformed(evt);
            }
        });

        labIndication.setFont(new java.awt.Font("Tahoma", 0, 18));
        labIndication.setText("Voici un aperçu de votre carte postale:");

        panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 204, 255)));

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 692, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 476, Short.MAX_VALUE)
        );

        btnSuivant.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        btnSuivant.setText("Terminer");
        btnSuivant.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTerminerActionPerformed(evt);
            }
        });

        btnImprimer.setFont(new java.awt.Font("Tahoma", 0, 18));
        btnImprimer.setText("Imprimer");
        btnImprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimerActionPerformed(evt);
            }
        });

        btnSauvegarder.setFont(new java.awt.Font("Tahoma", 0, 18));
        btnSauvegarder.setText("Sauvegarder");
        btnSauvegarder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSauvegarderActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(labBandeau, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnPrecedent, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(198, 198, 198)
                .addComponent(btnRecommencer, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 192, Short.MAX_VALUE)
                .addComponent(btnSuivant, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labIndication)
                .addContainerGap(668, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnImprimer, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSauvegarder, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(136, 136, 136))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(labBandeau, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(labIndication)
                        .addGap(93, 93, 93)
                        .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(199, 199, 199)
                        .addComponent(btnImprimer, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnSauvegarder, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnPrecedent, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnRecommencer, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSuivant, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnPrecedentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrecedentActionPerformed
        controleur.trImpressionPrecedent();
}//GEN-LAST:event_btnPrecedentActionPerformed

    private void btnRecommencerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRecommencerActionPerformed
        controleur.trRecommencer();
}//GEN-LAST:event_btnRecommencerActionPerformed

    private void btnTerminerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTerminerActionPerformed
        controleur.trImpressionTerminer();
}//GEN-LAST:event_btnTerminerActionPerformed

    private void btnImprimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimerActionPerformed
      try
      {
        controleur.trImpressionImprimer(this.ecranImpressionCPD.getImageFinale().getScaledInstance(
              this.ecranImpressionCPD.getImageFinale().getWidth(),
              this.ecranImpressionCPD.getImageFinale().getHeight(),
              0
              )
       );
      }
      catch (Exception ex) {
        CCLogger.Write(Level.SEVERE, Outils.getStackTrace(ex));
        this.afficherMessageErreur("Une erreur est survenue lors de l'impression");
    }
}//GEN-LAST:event_btnImprimerActionPerformed

    private void btnSauvegarderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSauvegarderActionPerformed
        //Permet la recherche du dossier
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        
        int test = fileChooser.showSaveDialog(this);
        
        File directorySelected = null;
        if (test == JFileChooser.APPROVE_OPTION) {
            directorySelected= fileChooser.getSelectedFile();
        }
        else
            return;
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
            Date date = new Date();
            //Ecris l'image dans le système de fichiers.
            File saveFile = new File(directorySelected.getAbsolutePath() + dateFormat.format(date) + ".jpg");
            ImageIO.write(ecranImpressionCPD.getImageFinale(), "jpg", saveFile);
        } catch (IOException ex) {
            CCLogger.Write(Level.SEVERE, Outils.getStackTrace(ex));
            this.afficherMessageErreur("Une erreur est survenue lors de l'initialison de l'écran Import.");
        }       
        
    }//GEN-LAST:event_btnSauvegarderActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnImprimer;
    private javax.swing.JButton btnPrecedent;
    private javax.swing.JButton btnRecommencer;
    private javax.swing.JButton btnSauvegarder;
    private javax.swing.JButton btnSuivant;
    private javax.swing.JLabel labBandeau;
    private javax.swing.JLabel labIndication;
    private javax.swing.JPanel panel;
    // End of variables declaration//GEN-END:variables

}
